<?php

namespace App\Providers;

use App\Facade\UserFuncFacade;
use App\Models\CompanyDepartment\DepartmentModel;
use App\Models\Goods\GoodsModel;
use App\Models\Users\UsersModel;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;

class CustomerFormRequestProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        // 用户是否存在
        Validator::extend('user_id_has', function ($attribute, $value, $parameters, $validator) {
            // 在这里编写自定义规则的验证逻辑
            // 查询用户
            $comId = UserFuncFacade::getCurrentComId();
            $user = UsersModel::where('com_id',$comId)
                ->where('user_id',$value)
                ->select(['user_id'])->first();
            if(!$user){
                return false;
            }
            return true;
        });
        // 部门是否存在
        Validator::extend('dept_id_has', function ($attribute, $value, $parameters, $validator) {
            if($value!=0){
                // 查询用户
                $comId = UserFuncFacade::getCurrentComId();
                $user = DepartmentModel::where('com_id',$comId)
                    ->where('department_id',$value)
                    ->select(['department_id'])
                    ->first();
                if(!$user){
                    return false;
                }
            }
            return true;
        });
        // 商品是否存在
        // 部门是否存在
        Validator::extend('goods_can_order', function ($attribute, $value, $parameters, $validator) {
            if($value!=0){
                // 查询用户
                $comId = UserFuncFacade::getCurrentComId();
                $goods = GoodsModel::where('com_id',$comId)
                    ->where('goods_id',$value)
                    ->where('enable_status',1)
                    ->where('status',1)
                    ->select(['goods_id'])
                    ->first();
                if(!$goods){
                    return false;
                }
            }
            return true;
        });
    }
}
